package org.sample.thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MultiThreadShareData {

    private Lock lock = new ReentrantLock();

    private int i = 0;

    public static void main(String[] args) {
        MultiThreadShareData t = new MultiThreadShareData();
        Jian jian = t.new Jian();
        Jia jia = t.new Jia();

        for (int i = 0; i < 20; i++) {
            new Thread(jian).start();
            new Thread(jia).start();
        }
    }

    public synchronized void jian() {
        lock.lock();
        i--;
        System.out.println(Thread.currentThread().getName() + " " + i);
        lock.unlock();
    }

    public synchronized void jia() {
        i++;
        System.out.println(Thread.currentThread().getName() + " " + i);
    }

    class Jian implements Runnable {
        @Override
        public void run() {
            jian();
        }
    }

    class Jia implements Runnable {
        @Override
        public void run() {
            jia();
        }
    }

}